<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/14 0014
 * Time: 14:00
 */

namespace App\Console\Commands;


use App\Socket\Services\SocketServer;
use Illuminate\Console\Command;

class WebSocketCommand extends Command
{
    /**
     * 命令
     * @var string
     */
    protected $signature = 'websocket {action} {--debug}';

    /**
     * 命令说明
     * @var string
     */
    protected $description = 'websocket服务命令';

    /**
     * 执行控制台命令。
     *
     * @return mixed
     */
    public function handle()
    {
        $action = $this->argument('action');
        //验证存放pid的目录是否存在，不存在需要创建
        $folder = storage_path('framework/server');
        if(!is_dir($folder)){
            mkdir($folder,0755,true);
        }
        $options = $this->options();
        if (isset($options['debug']) && $options['debug'] == 1) {
            config(['socket.websocket.options.daemonize' => false]);
        }
        switch ($action) {
            case 'start':
                SocketServer::instance()->start();
                break;
            case 'stop':
                SocketServer::instance()->stop();
                break;
            case 'restart':
                SocketServer::instance()->restart();
                break;
            default:
                echo "------------欢迎使用swoole------------\n";
                echo "使用命令php artisan websocket 运行参数为:\n";
                echo "start            启动websocket\n";
                echo "stop             停止websocket\n";
                echo "restart          重启websocket\n";
                echo "可选参数:\n";
                echo "--debug          调试模式\n";
        }
    }
}